class Solution
{
public:
    int similarPairs(vector<string> &words)
    {
        for (auto &word : words)
        {
            set<char> s(word.begin(), word.end());
            word = string(s.begin(), s.end());
        }
        int pairCount = 0;
        for (int i = 0; i < words.size(); ++i)
        {
            for (int j = i + 1; j < words.size(); ++j)
            {
                if (words[i] == words[j])
                {
                    ++pairCount;
                }
            }
        }
        return pairCount;
    }

    // 超时
    int similarPairs(vector<string> &words)
    {
        auto isSimilar = [&](int i, int j) -> bool
        {
            auto s1 = set<char>(words[i].begin(), words[i].end());
            auto s2 = set<char>(words[j].begin(), words[j].end());
            if (s1.size() != s2.size())
            {
                return false;
            }
            for (auto c : s1)
            {
                if (!s2.count(c))
                {
                    return false;
                }
            }
            return true;
        };
        int pairCount = 0;
        for (int i = 0; i < words.size(); ++i)
        {
            for (int j = i + 1; j < words.size(); ++j)
            {
                if (isSimilar(i, j))
                {
                    ++pairCount;
                }
            }
        }
        return pairCount;
    }
};